clear
clear all
cap log close

log using "log-files/3_Table_1.smcl", replace smcl

timer on 2

* Loading rdrobust_export, which is a program written by us to export results
* from rdrobust
cap program drop rdrobust_export
include "codes/auxiliary_programs/rdrobust_export.ado"

* Loading myround, which is a program written by us to round numbers
cap program drop myround
include "codes/auxiliary_programs/myround.ado"

*---------*
* Table 1 *
*---------*
	* First cutoff: pcs=0.95; this is the cutoff studied in the OAS report
	*---------------------------------------------------------------------
	use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear

	* Sort order within identical time stamps affects results
	* We use a sort order that gives us the median estimate (see Appendix G)
	gen row = 3
	merge m:1 row using "data/outputs/RD_sortings_mediandraw.dta"
	drop if _merge == 2
	drop _merge row

	merge 1:1 NumMesa sorting using "data/outputs/randomdraws_rd.dta"
	drop if _merge == 2
	drop _merge sorting

	keep Pais-Nulos trep_date_verificador random_draw

	* Measures that DO USE Blancos and Nulos (sorted)
	gen total_so = CC + FPV + MTS + UCS + MAS + F + PDC + MNR + PANBOL + ///
		Blancos + Nulos
	*---*
	gen mshare_so = MAS / total_so
	gen cshare_so = CC / total_so
	sort trep_date_verificador random_draw
	gen cumsumtotal_so = sum(total_so)
	*---*
	egen maxcumsumtotal_so = max(cumsumtotal_so)
	gen pcs_so = cumsumtotal_so / maxcumsumtotal_so
	drop cumsumtotal_so maxcumsumtotal_so

	* Extracting the time stamp of the pcs cutoff of 0.95
	sum pcs if pcs >= 0.95
	local cutoff = r(min)

	gen hour = hh(trep_date) if pcs == `cutoff'
	gen min = mm(trep_date) if pcs == `cutoff'
	gen secs = ss(trep_date) if pcs == `cutoff'
	gen date = dofc(trep_date) if pcs == `cutoff'
	format date %td
	gen day = day(date) if pcs == `cutoff'
	gen month = month(date) if pcs == `cutoff'
	gen year = year(date) if pcs == `cutoff'

	global date ""
	foreach g in month day year hour min secs {
		quietly sum `g'
		local loc_`g' = r(mean)
		drop `g'
		global date "$date `loc_`g''"
	}
	drop date

	* Generating a variable to cluster at the precinct level
	egen reci_id = group(Pais Dep Prov Muni Loc Reci)
	
	* Getting the RD estimate
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("replace") file("outputs/table1") ///
		colstring("Cutoff 0.95 (wrong) - Time $date") ///
		vce(cluster reci_id)

	* Second cutoff: we use the pcs corresponding to 20oct2019 19:40:57
	*------------------------------------------------------------------
	use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear

	gen row = 1
	merge m:1 row using "data/outputs/RD_sortings_mediandraw.dta"
	drop if _merge == 2
	drop _merge row

	merge 1:1 NumMesa sorting using "data/outputs/randomdraws_rd.dta"
	drop if _merge == 2
	drop _merge sorting

	keep Pais-Nulos trep_date_verificador random_draw

	* Measures that DO USE Blancos and Nulos (sorted)
	gen total_so = CC + FPV + MTS + UCS + MAS + F + PDC + MNR + PANBOL + ///
		Blancos + Nulos
	*---*
	gen mshare_so = MAS / total_so
	gen cshare_so = CC / total_so
	sort trep_date_verificador random_draw
	gen cumsumtotal_so = sum(total_so)
	*---*
	egen maxcumsumtotal_so = max(cumsumtotal_so)
	gen pcs_so = cumsumtotal_so / maxcumsumtotal_so
	drop cumsumtotal_so maxcumsumtotal_so

	* Extracting the time stamp of the cutoff of the shutdown
	sum pcs if trep_date_verificador >= tc(20oct2019 19:40:57)
	local cutoff = r(min)

	* Generating a variable to cluster at the precinct level
	egen reci_id = group(Pais Dep Prov Muni Loc Reci)

	* Getting the RD estimate
	rdrobust mshare pcs, c(`cutoff') vce(cluster reci_id)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("append") file("outputs/table1") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57") ///
		vce(cluster reci_id)

	* Formatting the results table
	*-----------------------------
	use "outputs/table1.dta", clear
	drop obs band_r conv_*

	foreach x of varlist estim-rob_ci_high {
		myround `x', decimals(3)
	}
	tostring obs_l obs_r, force replace

	gen rob_ci = "[" + rob_ci_low + ", " + rob_ci_high + "]"
	drop rob_ci_low rob_ci_high

	split colstring, p("-")
	drop colstring

	rename colstring2 time
	replace time = subinstr(time,"Time ","",.)
	split time, p(" ")
	split time6, p(".")
	drop time
	replace time5 = "0" + time5 if length(time5) == 1
	gen time = time1 + "/" + time2 + "/" + time3 + " " + time4 + ":" + time5 + ///
		":" + time61
	drop time1 time2 time3 time4 time5 time6*

	split colstring1, p(" ")
	drop colstring1 colstring11 colstring13
	rename colstring12 cutoff
	destring cutoff, replace
	myround cutoff, decimals(3)

	replace obs_l=subinstr(obs_l,"13","1,3",.)
	replace obs_l=subinstr(obs_l,"12","1,2",.)
	replace obs_r=subinstr(obs_r,"13","1,3",.)
	replace obs_r=subinstr(obs_r,"12","1,2",.)

	egen c = concat(cutoff time estim band_l rob_pv rob_ci obs_l obs_r), ///
		punct(" & ")
	keep c
	replace c = c + " \\"

	outsheet using "outputs/generated/Table_1.tex", replace noquote ///
		nolabel noname
	erase "outputs/table1.dta"

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
